//
//  UITabBarItem+Animation.m
//  ReviewDemo
//
//  Created by 龙 on 2018/5/10.
//  Copyright © 2018年 long. All rights reserved.
//

#import "UITabBarItem+Animation.h"
#import <objc/runtime.h>
#import <SDWebImage/UIImage+GIF.h>
@implementation UITabBarItem (Animation)

- (void)setObser
{
    
}

- (void)startAnimation
{
    if (self.lotView == nil)
    {
        NSString *resourceName = [self getJsonDataName];
        if (resourceName.length < 1)
        {
            return;
        }
        
        NSString *path = [kResourceBundle pathForResource:resourceName ofType:@"json"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.lotView = [LOTAnimationView animationFromJSON:dict inBundle:kResourceBundle];
    }
    
    __weak typeof(self) weakSelf = self;
    id contentView = [self valueForKey:@"view"];
    if (contentView && [contentView isKindOfClass:NSClassFromString(@"UITabBarButton")])
    {
        UIButton *button = (UIButton *)contentView;
        [button.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull buttonSubview, NSUInteger idx, BOOL * _Nonnull stop)
        {
            if ([buttonSubview isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")])
            {
                buttonSubview.hidden = YES;
                weakSelf.lotView.frame = buttonSubview.frame;
                [buttonSubview.superview addSubview:weakSelf.lotView];
                [weakSelf.lotView playWithCompletion:^(BOOL animationFinished) {
                    buttonSubview.hidden = NO;
                    [weakSelf.lotView removeFromSuperview];
                    
                    NSLog(@"------- 动画完成 -------");
                    [self logSelfInfo];
                }];
                NSLog(@"------- 动画进行 -------");
                [self logSelfInfo];

                *stop = YES;
            }
        }];
    }
    
    
}

- (void)stopAnimation
{
    if (self.lotView && self.lotView.isAnimationPlaying)//正在动画
    {
        [self.lotView stop];
        
        NSLog(@"------- stopAnimation -------");
        [self logSelfInfo];

        [self.lotView.superview.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull buttonSubview, NSUInteger idx, BOOL * _Nonnull stop)
        {
            if ([buttonSubview isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")])
            {
                buttonSubview.hidden = NO;
                *stop = YES;
            }
        }];
        [self.lotView removeFromSuperview];
    }
}

- (void)setLotView:(LOTAnimationView *)lotView
{
    objc_setAssociatedObject(self, @selector(lotView), lotView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (LOTAnimationView *)lotView
{
    return objc_getAssociatedObject(self, _cmd);
}

- (BOOL)respondsToSelector:(SEL)aSelector
{
    NSLog(@"aSelector ====== %@",[NSString stringWithUTF8String:sel_getName(aSelector)]);
    return YES;
}

- (void)logSelfInfo
{
    UITabBarItem;
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    unsigned int count = 0;
    Ivar *iavrList = class_copyIvarList(self.class, &count);
    for (int i = 0; i < count; i ++)
    {
        Ivar ivar = iavrList[i];
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        
        if ([self valueForKey:key] )
        {
            [mutableDict setObject:[self valueForKey:key] forKey:key];
        }
        else
        {
            
        }
        
    }
    free(iavrList);

    UIButton *button =(UIButton *)[self valueForKey:@"_view"];
    
    [self logoImageView];
    

//    NSLog(@"%f : %d-------- \n %@ ==== %@",button.frame.origin.x,button.highlighted,mutableDict,button.subviews);
}

- (NSString *)getJsonDataName
{
    NSString *gifName = @"";
    
    if ([self.title isEqualToString:@"底层"])
    {
        gifName = @"tabbar_data_1";
    }
    else if ([self.title isEqualToString:@"功能"])
    {
        gifName = @"tabbar_data_2";
    }
    else if ([self.title isEqualToString:@"解读"])
    {
        gifName = @"tabbar_data_3";
    }
    else if ([self.title isEqualToString:@"我的"])
    {
        gifName = @"tabbar_data_5";
    }

    return gifName;
}

- (id)valueForUndefinedKey:(NSString *)key
{
    return [NSNull null];
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
}

- (UIImageView *)logoImageView
{
    UIButton *button =(UIButton *)[self valueForKey:@"_view"];
    __block UIImageView *imageView = nil;
    [button.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
     {
         if ([obj isKindOfClass:[UIImageView class]])
         {
             imageView = obj;
             * stop = YES;
         }
     }];
//    NSLog(@"logoImageView ----- %@",imageView);
    return imageView;
    
}

@end
